<template>
  <div>
    <div class="navbar">
      <van-nav-bar
        fixed
        placeholder
        safe-area-inset-top
        title="暖暖房屋"
        @click-left="back"
      >
        <template #left>
          <van-icon name="arrow-left" size="16px" color="#333" />
        </template>
        <template #right>
          <van-icon name="ellipsis" @click="isShowShare" />
        </template>
      </van-nav-bar>
    </div>
    <img :src="url" alt="" width="100%" />
    <van-share-sheet
      v-model="showShare"
      title="立即分享给好友"
      :options="options"
      @select="onSelect"
    />
  </div>
</template>

<script>
import { getUserId } from "@/utils/auth";
import { shareIntegral } from "@/apis/integral";
import { _local } from "@/utils/storage";

export default {
  data() {
    return {
      showShare: false,
      options: [[{ name: "微信", icon: "wechat" }]],
      url: "",
    };
  },
  created() {
    const _this = this;
    _this.$plusReady(() => {
      plus.share.getServices(function (e) {
        _this.shareData = e;
        for (var i in e) {
          if (e[i].id == "weixin") {
            _this.sharewx = e[i];
          }
        }
      });
    });
    _this.getActivity();
  },
  methods: {
    getActivity() {
      fetch(`https://api.nuanuan.net/index/banner?type=${2}`, {
        method: "GET",
      })
        .then((res) => res.json())
        .then((res) => {
          console.log("活动图", res);
          this.url = res.data[0].page_url;
        });
    },
    back() {
      this.$router.go(-1);
    },
    isShowShare() {
      const userId = getUserId();
      if (userId) {
        this.showShare = true;
      } else {
        this.$router.push("/login");
      }
    },
    shareGetIntegral() {
      const _this = this;
      shareIntegral({ type: 5 }).then((res) => {
        if (res.code === 200) {
          _this.$toast("分享成功，获得1积分");
        }
      });
    },
    onSelect(option) {
      const _this = this;
      _this.showShare = false;
      if (option.name === "微信") {
        if (_local.get("role") == 1) {
          var msg = {
            type: "web",
            title: "亲,这里有0中介费的直租好房！",
            content: `租房神器！房东直租没有中介费，价格比长租公寓便宜`,
            thumbs: ["https://image.nunbdc.net/image/152-152.png"],
            href: "https://html.nunbdc.net/share2.html",
            extra: { scene: "WXSceneSession" },
          };
        }
        if (_local.get("role") == 5) {
          var msg = {
            type: "web",
            title: "亲，这里有业主直卖房源！",
            content: `0中介费,价格和业主直接谈，省时省心更省钱！`,
            thumbs: ["https://image.nunbdc.net/image/152-152.png"],
            href: "https://html.nunbdc.net/maishare.html",
            extra: { scene: "WXSceneSession" },
          };
        }
        if (_local.get("role") == 2 || _local.get("role") == 3) {
          var msg = {
            type: "web",
            title: "亲,这里可以直卖直租0中介费！",
            content: `租客多、买家多、快速交易、快速变现！省时省心更赚钱`,
            thumbs: ["https://image.nunbdc.net/image/152-152.png"],
            href: "https://html.nunbdc.net/FangShare.html",
            extra: { scene: "WXSceneSession" },
          };
        }
        if (_local.get("role") == 4) {
          var msg = {
            type: "web",
            title: "兄弟，我在这里可以抢单抢红包！",
            content: `经纪人赚钱神器！全平台自由协作不分佣不扣佣，赚钱多结算快！`,
            thumbs: ["https://image.nunbdc.net/image/152-152.png"],
            href: "https://html.nunbdc.net/agent6.html",
            extra: { scene: "WXSceneSession" },
          };
        }
        _this.sharewx.send(
          msg,
          function () {
            if (_local.get("role") == 1 || _local.get("role") == 4) {
              _this.shareGetIntegral();
            }
            _this.$toast.success("分享成功");
          },
          function () {
            _this.$toast("分享失败");
          }
        );
      }
    },
  },
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
img {
  display: block;
}
</style>